package com.oneinlet

import org.junit.Test
import java.lang.reflect.Field
import java.math.BigDecimal
import java.util.*


/**
 * Created by WangZiHe on 2018/12/5/005
 * QQ/WeChat:648830605
 * QQ-Group:368512253
 * Blog:www.520code.net
 * Github:https://github.com/yancheng199287
 */

class TestYCReflect {
    @Test
    fun getPropertyName() {
        val fields: Array<Field> = User::class.java.declaredFields
        for (field in fields) {
            println(field.name)
        }
    }

    @Test
    fun getPropertyValue() {
        val user = User(10, "张思淼", 15, BigDecimal.valueOf(100.00))
        val fields: Array<Field> = user::class.java.declaredFields
        var map = HashMap<String, Object>(fields.size)
        for (field in fields) {
            field.isAccessible = true
            println(field.name + "---" + field.get(user))
        }
    }


    @Test
    fun callmethod() {
        YCReflect.callObjectMethod(User::class.java, "introduce", "aaa", 20)
    }


    @Test
    fun setProperty() {
        val user = User()
        YCReflect.setPropertyValue<User>(user, "name", "胡啸天")
        println(user.name)
    }

    @Test
    fun setMoreProperty() {
        val user = User()
        val map = mapOf("id" to 300, "age" to 33, "name" to "张晓华", "money" to BigDecimal.valueOf(3666.66))
        YCReflect.setPropertyValue<User>(user, map)
        println(YCString.toString(user))
    }


    @Test
    fun copyObjectToOther() {
        val user = User(10, "", 15, BigDecimal.valueOf(100.00))
        val normalUser = NormalUser("", 1235555)
        user.normalUser = normalUser

        var vipUser = VipUser()

        println("没有复制之前：")
        println(user)
        println(vipUser)

        vipUser = YCReflect.copyObjectToOther(user, vipUser)

        println("已经复制之后：")
        println(user)
        println(vipUser)

    }


}


